package com.bifrost.admin.controller;

import com.bifrost.annotation.CurrentAdmin;
import com.bifrost.common.base.ServerResponse;
import com.bifrost.constant.Constant;
import com.bifrost.entity.User;
import com.bifrost.model.param.PermissionParam;
import com.bifrost.service.AdminUserService;
import com.bifrost.service.PermissionService;
import com.bifrost.util.BeanValidator;
import com.bifrost.validator.group.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * Author by yanxiaobo
 * Date on 2018/4/10$.
 */
@RestController
@RequestMapping("/api/permission")
@Api("权限相关接口")
public class PermissionController {
    @Autowired
    PermissionService permissionService;
    @Autowired
    private AdminUserService userService;

    /**
     * 获取权限list
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ApiOperation("获取所有权限")
    public ServerResponse list() {
       return permissionService.list();
    }


    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation("新增权限点")
    public ServerResponse add(PermissionParam param) {
        Map<String,String> errors = null;
        if(null == param || null==param.getType())
            return ServerResponse.createByErrorMsg("参数错误");
        if(Constant.Permission.PERMISSION_TYPE_INDEX == param.getType()) {
            errors = BeanValidator.validate(param, IndexAdd.class);
        } else if(Constant.Permission.PERMISSION_TYPE_MENU == param.getType()) {
            errors = BeanValidator.validate(param, MenuAdd.class);
        } else {
            errors = BeanValidator.validate(param, ButtonAdd.class);
        }
        if(!errors.isEmpty())
            return ServerResponse.createByError(errors.values());
        return permissionService.add(param);
    }

    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ApiOperation("修改权限")
    public ServerResponse edit(PermissionParam param, @CurrentAdmin Long currentUser) {
        Map<String,String> errors = null;
        if(null == param || null==param.getType())
            return ServerResponse.createByErrorMsg("参数错误");
        if(null == param || null==param.getType())
            return ServerResponse.createByErrorMsg("参数错误");
        if(Constant.Permission.PERMISSION_TYPE_INDEX == param.getType()) {
            errors = BeanValidator.validate(param, IndexUpdate.class);
        } else if(Constant.Permission.PERMISSION_TYPE_MENU == param.getType()) {
            errors = BeanValidator.validate(param, MenuUpdate.class);
        } else {
            errors = BeanValidator.validate(param, ButtonUpdate.class);
        }
        if(!errors.isEmpty())
            return ServerResponse.createByError(errors.values());
        return permissionService.update(param,userService.findById(currentUser) );
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ApiOperation("删除权限")
    public ServerResponse delete(Integer menuId) {
        if(null == menuId)
            return ServerResponse.createByErrorMsg("删除操作必须指定具体的权限");
        return permissionService.delete(menuId);
    }

    /**
     * 获取权限list
     * @return
     */
    @RequestMapping(value = "/list/add",method = RequestMethod.GET)
    @ApiOperation("获取所有权限")
    public ServerResponse listForRoleAdd() {
        return permissionService.list();
    }

    /**
     * 获取用户持有的权限和菜单
     * @param userId
     * @return
     */
    @RequestMapping(value = "/user",method = RequestMethod.GET)
    @ApiOperation("获取用户持有的权限和菜单")
    public ServerResponse getUserPermissions(Integer userId) {
        return permissionService.getUserPermissions(userId);
    }
    

}
